package com.iot.inter.controller.newinterface.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.controller.newinterface.extra.ExtraPackagesSyncController;
import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newrequest.order.batchorder.request.RequestBatchOrderNew;
import com.iot.inter.entity.newrequest.order.batchorder.request.RequestDeviceList;
import com.iot.inter.entity.newrequest.order.batchorder.response.ResponseBatchOrderNew;
import com.iot.inter.interfaceprocessing.newinterface.TurnToOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 去北京做 3.25	批量订购套餐:2.0接口
 * <p>
 * 接入方可以通过调用该接口，接入方可以为其拥有的设备，批量订购套餐。订购规则如下：
 * 	通过该接口可以批量订购流量套餐，也可以批量订购日程套餐；
 * 	如果订购流量套餐时，同时要求立即开通套餐，那么新订购的套餐将立即被开通。OTA设备订购的副号流量套餐，不支持立即开通操作；
 * 	如果订购流量套餐时，不进行立即开通，那么IOT系统不开通此套餐；
 * 	对处于库存期的设备订购的sheb流量套餐订单，如果要求支持测试流量，则将作为首单处理；
 *
 * @author lm
 * @date 2019/3/4
 */
@RestController
@Api(description = "3.25 批量订购套餐")
public class BatchOrderPackageController {

    private static Logger logger = Logger.getLogger(BatchOrderPackageController.class);

    @CrossOrigin
    @RequestMapping(value = "batchOrder", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.25 批量订购套餐", response = ResponseBatchOrderNew.class)
    public String batchOrder(@ApiParam @RequestBody RequestBatchOrderNew request) {

        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);
        if (requestStr != null) {

            logger.info("【批量订购套餐】请求：" + requestStr);
            RequestBatchOrderNew requestPackage = JSONArray.parseObject(requestStr, RequestBatchOrderNew.class);
            String globalOrder = requestPackage.getGlobalOrder();
            String packageName = requestPackage.getPackageName();
            String packageCode = requestPackage.getPackageCode();
            String packageType = requestPackage.getPackageType();
            String orderPeriod = requestPackage.getOrderPeriod();
            String amount = requestPackage.getAmount();
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();

            List<RequestDeviceList> deviceList = requestPackage.getDeviceList();

            for (RequestDeviceList de : deviceList) {

                String deviceId = de.getDeviceId();

                //现在去北京拿单号
                TurnToOrderService turnToOrderService = new TurnToOrderService();
                OrderPaymentManager manager = turnToOrderService.batchOrderPackage(deviceList, globalOrder, packageName, packageCode, packageType, orderPeriod, amount, secret, partnerCode);

                if (manager == null) {

                    ResponseBatchOrderNew responseOrder = new ResponseBatchOrderNew();
                    responseOrder.setRetCode("9999");
                    responseOrder.setResultInfo("接口返回有误");
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(responseOrder);
                    logger.info("【批量订购套餐】响应：" + jsonString);
                    return jsonString;


                } else if (true == Constant.RET_CODE_0000.equalsIgnoreCase(manager.getRetCode())) {

                    ResponseBatchOrderNew responseOrder = new ResponseBatchOrderNew();
                    responseOrder.setRetCode("0000");
                    responseOrder.setResultInfo("success");

                    jsonString = JSON.toJSONString(responseOrder);
                    logger.info("【批量订购套餐】成功响应：" + jsonString);
                    return jsonString;

                } else {
                    ResponseBatchOrderNew responseOrder = new ResponseBatchOrderNew();
                    responseOrder.setRetCode(manager.getRetCode());
                    responseOrder.setResultInfo(manager.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(responseOrder);
                    logger.info("【批量订购套餐】响应：" + jsonString);
                    return jsonString;

                }
            }
        } else {
            ResponseBatchOrderNew responseOrder = new ResponseBatchOrderNew();
            responseOrder.setRetCode("9998");
            responseOrder.setResultInfo("请求报文为空");
            // 返回给调用者的报文
            jsonString = JSON.toJSONString(responseOrder);
            logger.info("【批量订购套餐】响应：" + jsonString);
            return jsonString;
        }

        return null;
    }
}
